package nollab.nolapp.component;

import me.twoday.library.mine.common.ActivityUtil;
import me.twoday.library.mine.common.SharedPreferenceUtil;
import me.twoday.library.mine.common.base.BaseApplication;
import nollab.nolapp.R;
import nollab.nolapp.app.fflove.FFLoveIntroActivity;
import nollab.nolapp.app.toto.ToToIntroActivity;
import nollab.nolapp.domain.ServiceInfo;
import nollab.nolapp.listener.ServiceListener;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.Html;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 서비스 필드를 컨트롤 하는부분 
 *
 * @author twoday.me@gmail.com 
 * @date 2012. 6. 25.
 */
public class ServiceView extends LinearLayout implements OnClickListener, ServiceListener
{
	private static final String TAG = "ServiceView";

	private View serviceView;

	public static final String STATUS_ADDED = "added";
	public static final String STATUS_ADD = "add";
	
	
	/**
	 * 서비스 추가가능 상태 
	 */
	private ImageView ivServiceAdd;

	/**
	 * 서비스 추가된후 상태  
	 */
	private LinearLayout ivServiceAdded;

	private boolean visible;

	/**
	 * 서비스 이름 
	 */
	private TextView tServiceName;

	/**
	 * 서비스 뷰 생성자 
	 * @param context
	 */
	public ServiceView(Context context)
	{
		super(context);
		init(context);
	}

	/**
	 * 서비스 뷰 생성자  
	 * @param context
	 * @param attrs
	 */
	public ServiceView(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		init(context);
	}

	/**
	 * 서비스 뷰를 초기화 한다. 
	 * 
	 * @param context
	 */
	private void init(Context context)
	{
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		serviceView = inflater.inflate(R.layout.service_view, null);
		
		ivServiceAdd = (ImageView)serviceView.findViewById(R.id.service_iv_service_add);
		ivServiceAdded = (LinearLayout)serviceView.findViewById(R.id.service_iv_service_added);
		tServiceName = (TextView)serviceView.findViewById(R.id.service_tv_service_name);
		ivServiceAdd.setOnClickListener(this);
		ivServiceAdded.setOnClickListener(this);
		this.addView(serviceView);
		initView();
	}


	/**
	 * 실제 서비스를 시작합니다.  
	 */
	private void startToService(ServiceInfo serviceInfo)
	{
//		Bundle extras = new Bundle();
//		extras.putInt("farmIndex", farmInfo.getFarmIndex());
//
//		// ??Ъ 援ъ? ???吏?? ?대?
//		ActivityUtil activityUtil = new ActivityUtil(BaseApplication.getTopActivity());
//		activityUtil.setTargetActivity(CropListActivity.class).setBundle(extras).setRequestCode(FarmListener.CODE_SEED).show();
	}

	
	@Override
	public void onClick(View v)
	{
		ServiceInfo info = (ServiceInfo) serviceView.getTag();
		switch (v.getId()) {
		case R.id.service_iv_service_add:
			onClickAdd(info);
			break;
		case R.id.service_iv_service_added:
			onClickAdded(info);
			break;

		default:
			break;
		}
	}
	
	/**
	 * 추가된 앱이 선택됬을경우 
	 */
	private void onClickAdded(final ServiceInfo serviceInfo) 
	{
		int serviceId = serviceInfo.getServiceId();
		Intent intent = null;
		Activity activity = BaseApplication.getTopActivity();
		Log.e("coooooooool", serviceId+"");
		
		if(serviceId==1)
		{
			intent = new Intent(activity, FFLoveIntroActivity.class);
		}
		else if(serviceId==2)
		{
			intent = new Intent(activity, ToToIntroActivity.class);
		}
		else
		{
			Log.e(TAG, "intent is null");
		}
		activity.startActivity(intent);
	}

	private void onClickAdd(final ServiceInfo serviceInfo)
	{
		String message = "어플을 추가 하시겠습니까?";
		
		new AlertDialog.Builder(BaseApplication.getTopActivity())
			.setTitle(BaseApplication.getConfigPersistent("defaultName"))
			.setMessage(Html.fromHtml(message))
			.setPositiveButton("어플추가", new DialogInterface.OnClickListener()
			{
				@Override
				public void onClick(DialogInterface dialog, int which)
				{
					serviceInfo.setStatus(STATUS_ADDED);
					
				}
			})
			.setNegativeButton("취소", new DialogInterface.OnClickListener()
			{
				@Override
				public void onClick(DialogInterface dialog, int which)
				{
					dialog.dismiss();
				}
			})
			.show();
	}

	/**
	 * 화면 노출상태를 반환한다. 
	 * @return
	 */
	public boolean isVisible()
	{
		return visible;
	}

	/**
	 * 화면 노출상태를 설정한다. 
	 * 
	 * @param visible
	 */
	public void setVisible(boolean visible)
	{
		this.visible = visible;
	}

	/**
	 * 뷰를 초기화 한다. 
	 */
	public void initView()
	{
		ivServiceAdd.setVisibility(View.INVISIBLE);
		ivServiceAdded.setVisibility(View.INVISIBLE);
		tServiceName.setVisibility(View.INVISIBLE);
	}

	@Override
	public void setVisibleAdd() 
	{
		ivServiceAdd.setVisibility(View.VISIBLE);
		ivServiceAdded.setVisibility(View.INVISIBLE);
		tServiceName.setVisibility(View.INVISIBLE);
	}
	
	@Override
	public void setVisibleAdded() 
	{
		ivServiceAdd.setVisibility(View.INVISIBLE);
		ivServiceAdded.setVisibility(View.VISIBLE);
		tServiceName.setVisibility(View.VISIBLE);
	}

	@Override
	public void onServiceUpdate(ServiceInfo serviceInfo) 
	{
		
		String status = serviceInfo.getStatus();
		if(status.equals(STATUS_ADD))
		{
			ivServiceAdd.setVisibility(View.VISIBLE);
			ivServiceAdded.setVisibility(View.INVISIBLE);
			tServiceName.setVisibility(View.INVISIBLE);
		}
		else if(status.equals(STATUS_ADDED))
		{
			
			SharedPreferenceUtil pref = SharedPreferenceUtil.getInstance(BaseApplication.getContext());
			int serviceId = ((ServiceInfo)serviceView.getTag()).getServiceId();
			ivServiceAdd.setVisibility(View.INVISIBLE);
			ivServiceAdded.setVisibility(View.VISIBLE);
			tServiceName.setVisibility(View.VISIBLE);
			if(serviceId==1)
			{
				tServiceName.setText("친친사");
			}
			else if(serviceId==2)
			{
				tServiceName.setText("토토");
			}
		}
	}

	@Override
	public void setTag(ServiceInfo serviceInfo) 
	{
		serviceView.setTag(serviceInfo);
	}
	
	/**
	 * 새로운 액티비티를 생성한다.
	 * 기존 BaseActivity에 정의 되어 있지만 protect로 되어있어서
	 * 여기에서 재정의 해줬으며, 4개의 Activity간에 데이터 이동이 없기 때문에
	 * setBundle을 해주지 않았다.
	 * 
	 * @return 액티비티 관리자
	 */
	public final ActivityUtil getNewActivity()
	{
		return new ActivityUtil(BaseApplication.getTopActivity());
	}
	
}